package com.sky.mapper;

import com.github.pagehelper.Page;
import com.sky.dto.GoodsSalesDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.entity.OrderDetail;
import com.sky.entity.Orders;
import com.sky.vo.OrderVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 订单表 Mapper 接口
 * </p>
 *
 * @author lwd
 * @since 2023-04-24
 */
@Mapper
public interface OrdersMapper {
    /**
     * 保存
     * @param orders
     */
    void save(Orders orders);

    /**
     * 根据订单号和用户id查询订单
     * @param orderNumber
     * @param userId
     */
    @Select("select * from orders where number = #{orderNumber} and user_id= #{userId}")
    Orders getByNumberAndUserId(String orderNumber, Long userId);

    /**
     * 修改订单信息
     * @param orders
     */
    void update(Orders orders);

    /**
     * 根据id查询订单
     * @param id
     * @return
     */
    @Select("select * from orders where id = #{id}")
    Orders getById(Long id);

    /**
     * 历史订单查询
     * @param ordersPageQueryDTO
     * @return
     */
    Page<OrderVO> historyOrders(OrdersPageQueryDTO ordersPageQueryDTO);

    /**
     * 条件查询订单
     * @param ordersPageQueryDTO
     * @return
     */
    List<OrderVO> getByCondition(OrdersPageQueryDTO ordersPageQueryDTO);

    /**
     * 查询订单详情
     * @param id
     * @return
     */
    List<OrderDetail> getDetailById(Long id);

    /**
     * 查询各个状态的订单数量统计
     * @param orders
     * @return
     */
    @Select("select count(*) from orders where status = #{status}")
    Integer numOfToBeConfirmed(Orders orders);
    @Select("select count(*) from orders where status = #{status}")
    Integer numOfConfirmed(Orders orders);
    @Select("select count(*) from orders where status = #{status}")
    Integer numOfDeliveryInProgress(Orders orders);

    /**
     * 根据id查询订单信息
     * @param id
     * @return
     */
    @Select("select * from orders where id=#{id}")
    Orders getCancelById(Long id);

    @Select("select * from orders where status = #{status} and order_time < #{dateTime}")
    List<Orders> queryByStatusAndOrderTime(Integer status, LocalDateTime dateTime);

    void batchUpdate(@Param("ids") List<Long> ordersList,@Param("o") Orders order);

    Double sumAmountByParams(Map paramMap);

    List<Map> groupSumAmountByParams(Map paramMap);

    List<Map> groupCountOrderByParams(Map params);

    List<GoodsSalesDTO> groupSumNumberByParams(Map params);

    /**
     * 根据动态条件统计营业额数据
     * @param map
     * @return
     */
    Double sumByMap(Map map);

    /**
     * 根据动态条件统计订单数量
     * @param map
     * @return
     */
    Integer countByMap(Map map);
}
